//
//  WBBaseViewController.swift
//  Weibo
//
//  Created by 夏灵烽 on 2025/1/1.
//

import UIKit



class WBBaseViewController: UIViewController, UITableViewDelegate, UITableViewDataSource
{
    
    // MARK: - 属性
    var userLogon = false
    
    var tableView: UITableView?
    var refreshControl: UIRefreshControl?
    
    var isPullup = false
    
    var visitorInfoDict: [String: String]?
    
    
    // MARK: - 重写title的set方法
    
    override var title: String? {
        didSet {
            navigationItem.title = title
        }
    }
    
    // MARK: - 入口
    override func viewDidLoad() {
        super.viewDidLoad()
                
        setupUI()
    }
    
    // MARK: - 界面选着
    
    func setupUI()
    {
        view.backgroundColor = UIColor.white
        
        if userLogon == false {
            setupVisitotView()
        } else {
            setupTableView()
        }
        
    }
    
    // MARK: - 访客视图
    func setupVisitotView() {
        
        let visitorView = WBVisitorView(frame: view.bounds)
        
        view.addSubview(visitorView)
        
        visitorView.visitorInfo = visitorInfoDict
        
        visitorView.registerButton.addTarget(self, action: #selector(clickRegister), for: .touchUpInside)
        visitorView.loginButton.addTarget(self, action: #selector(clickLogin), for: .touchUpInside)
        
        navigationItem.leftBarButtonItem = UIBarButtonItem(title: "注册", style: .plain, target: self, action: #selector(clickRegister))
        navigationItem.rightBarButtonItem = UIBarButtonItem(title: "登录", style: .plain, target: self, action: #selector(clickLogin))
        
    }
    
    @objc func clickRegister() {
        
    }
    
    @objc func clickLogin() {
        
    }
    
    // MARK: - 列表视图
    
    func setupTableView() {
        
        if tableView == nil 
        {
            tableView = UITableView(frame: view.bounds, style: .plain)
            
            tableView?.contentInsetAdjustmentBehavior = .never
            
            let navigationBarY: CGFloat = navigationController?.navigationBar.frame.origin.y ?? 98
            
            tableView?.contentInset = UIEdgeInsets(top: navigationBarY, left: 0, bottom: tabBarController?.tabBar.bounds.height ?? 49, right: 0)
            
            tableView?.delegate = self
            
            tableView?.dataSource = self
            
            view.insertSubview(tableView ?? UIView(), belowSubview: navigationController?.navigationBar ?? UIView())
        }
        
        if refreshControl == nil {
            refreshControl = UIRefreshControl()
            tableView?.addSubview(refreshControl ?? UIRefreshControl())
            refreshControl?.addTarget(self, action: #selector(loadData), for: .valueChanged)
        }
         
        loadData()
    }
    
    @objc func loadData()
    {
        refreshControl?.endRefreshing()
    }
    
    // MARK: - UITableViewDataSource
    
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return 0
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        return UITableViewCell()
    }
    
    // MARK: - UITableViewDelegate
    
    func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) {
        
        let row = indexPath.row
        
        let lastSectionIndex = tableView.numberOfSections - 1
        
        let lastRowIndex = tableView.numberOfRows(inSection: lastSectionIndex) - 1
        
        if lastRowIndex < 0 || lastSectionIndex < 0 {
            return
        }
        
        if row == lastRowIndex && isPullup == false {
            isPullup = true
            loadData()
        }
        
    }
}
